package com.qyhl.webtv.module_microvideo.shortvideo.play;

import android.util.Log;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoBean;
import com.qyhl.webtv.module_microvideo.common.VideoUrl;

import java.util.List;

public class ShortVideoPlayModel implements ShortVideoPlayContract.ShortVideoPlayModel {
    private ShortVideoPlayPresenter mPresenter;

    ShortVideoPlayModel(ShortVideoPlayPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(String workId, String phone) {
        EasyHttp.get(VideoUrl.GET_SHORT_VIDEO_DETAIL)
                .params("workId", workId)
                .params("phone", phone)
                .execute(new SimpleCallBack<ShortVideoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError("详情数据获取失败！");
                    }

                    @Override
                    public void onSuccess(ShortVideoBean shortVideoBean) {
                        if (shortVideoBean != null) {
                            mPresenter.setDetail(shortVideoBean);
                        } else {
                            mPresenter.setError("详情数据获取失败！");
                        }
                    }
                });

    }


    @Override
    public void doPraise(String workId, String phone) {

        EasyHttp.get(VideoUrl.GET_SHORT_VIDEO_PRAISE)
                .params("workId", workId)
                .params("phone", phone)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.praiseError("点赞失败，请稍后再试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult result = new Gson().fromJson(s, ApiResult.class);
                        if (result.getCode() == 200) {
                            mPresenter.praiseSuccess();
                        } else if (result.getCode() == 205) {
                            Log.d("praise", "已经点赞了！");
                        } else {
                            mPresenter.praiseError("点赞失败，请稍后再试！");
                        }
                    }
                });

    }

    @Override
    public void getData(String actId, final String page, int type) {
        String url = "";
        switch (type) {
            case 0://搜索（暂不支持）
                break;
            case 1://主题详情下作品
                url = VideoUrl.GET_SHORT_VIDEO_THEME_DETAIL_LIST;
                break;
            case 2://实时榜
                url = VideoUrl.GET_SHORT_VIDEO_HOUR_LIST;
                break;
            case 3://首页
                url = VideoUrl.GET_SHORT_VIDEO_HOME_LIST;
                break;
            case 4://个人作品
                url = VideoUrl.GET_SHORT_VIDEO_USER_LIST;
                break;
            case 5://个人榜单
                url = VideoUrl.GET_SHORT_VIDEO_USER_RANK;
                break;
            case 6://周榜
                url = VideoUrl.GET_SHORT_VIDEO_WEEK_LIST;
                break;
            case 7://总榜
                url = VideoUrl.GET_SHORT_VIDEO_TOTAL_LIST;
                break;
            case 8:
                url = VideoUrl.GET_SHORT_VIDEO_TOTAL_LIST_BY_TIME;
                break;
            default:
                url = VideoUrl.GET_SHORT_VIDEO_TOTAL_LIST;
                break;
        }

        EasyHttp.get(url)
                .params("actId", actId)
                .params("tagId", actId)
                .params("phone", actId)
                .params("page", page)
                .execute(new SimpleCallBack<List<ShortVideoBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setDataError("暂无更多内容！");
                        } else {
                            mPresenter.setDataError("获取失败！");
                        }
                    }

                    @Override
                    public void onSuccess(List<ShortVideoBean> shortVideoBeans) {
                        if (shortVideoBeans != null && shortVideoBeans.size() > 0) {
                            mPresenter.setData(shortVideoBeans);
                        } else {
                            mPresenter.setDataError("暂无更多内容！");
                        }
                    }
                });
    }

    @Override
    public void getFollowNum(String workId) {

        EasyHttp.get(VideoUrl.GET_SHORT_VIDEO_FOLLOW_NUM)
                .params("workId", workId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult result = new Gson().fromJson(s, ApiResult.class);
                        if (result.getCode() == 200) {
                            mPresenter.setFollowNum(result.getData().toString());
                        } else {
                            Log.d("short_video_follow", result.getCode() + "");
                        }
                    }
                });
    }
}
